package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_DeleteInventoryItem extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_DeleteInventoryItem.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      int itemObjectId = readD();
      L1ItemInstance item = pc.getInventory().getItem(itemObjectId);
      if (item == null || item.getCount() <= 0L)
        return; 
      int count = readD();
      if (count == 0)
        count = (int)item.getCount(); 
      if (!pc.isGm() && item.getItem().isCantDelete()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(125));
        return;
      } 
      Object[] petlist = pc.getPetList().values().toArray();
      Object[] array;
      int length = (array = petlist).length;
      int i = 0;
      while (i < length) {
        Object petObject = array[i];
        if (petObject instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)petObject;
          if (item.getId() == pet.getItemObjId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(125));
            return;
          } 
        } 
        i++;
      } 
      if (item.getId() == pc.getQuest().get_step(74)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(125));
        return;
      } 
      if (pc.getDoll(item.getId()) != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        return;
      } 
      if (item.isEquipped()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(125));
        return;
      } 
      if (item.getBless() >= 128) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
        return;
      } 
      if (item.isSeal()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(item.getLogName()) + "处于绑定状态！"));
        return;
      } 
      _log
        .info("人物:" + pc.getName() + "删除物品" + item.getItem().getName() + " 物品OBJID:" + item.getId());
      RecordTable.get().recordDeleItem(pc.getName(), item.getAllName(), (int)item.getCount(), item.getId(), 
          pc.getIp());
      pc.getInventory().removeItem(item, count);
      World.get().removeObject((L1Object)item);
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
